package cn.com.surker.control.service.impl;

import cn.com.surker.control.service.ErpControlInvoiceService;
import cn.com.surker.enums.StateEnum;
import cn.com.surker.control.entity.ControlInvoice;
import cn.com.surker.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 工控机自主生产发货信息 服务实现类
 * </p>
 *
 * @author admin
 * @since 2025-01-06
 */
@Service
public class ErpControlInvoiceImpl extends ControlInvoiceBaseImpl implements ErpControlInvoiceService {

	@Override
	public Date findMaxProTime(Long lineId) {
		Date proTime = controlInvoiceDao.findMaxProTime(lineId);
		return proTime;
	}

	@Override
	public Long findMaxProId(Long lineId) {
		Long proId = controlInvoiceDao.findMaxProId(lineId);
		return proId;
	}

	@Override
	public ControlInvoice queryByConId(String controlId) {
		if (StringUtils.isBlank(controlId)) {
			return null;
		}
		QueryWrapper<ControlInvoice> qw = new QueryWrapper<>();
		qw.eq("status", StateEnum.ENABLED.getCode());
		qw.eq("control_invoice_no", controlId);
		qw.last("limit 1");
		ControlInvoice controlInvoice = controlInvoiceDao.selectOne(qw);
		return controlInvoice;
	}

	@Override
	public IPage<ControlInvoice> findPage(String startTime, String endTime, String site, Integer conAuto, Integer linkErp, Integer pageIndex, Integer limit) {
		QueryWrapper<ControlInvoice> qw = new QueryWrapper<>();
		if (StringUtils.isNotBlank(startTime)) {
			qw.ge("pro_time", startTime);
		}
		if (StringUtils.isNotBlank(endTime)) {
			qw.lt("pro_time", endTime);
		}
		if (StringUtils.isNotBlank(site)) {
			qw.like("site", site);
		}
		if (conAuto != null) {
			qw.eq("con_auto", conAuto);
		}
		if (linkErp != null) {
			qw.eq("con_auto", 1);
			if (linkErp == 0) {
				//	未关联
				qw.isNull("link_erp");
			} else {
				//	已关联
				qw.eq("link_erp", 1);
			}
			// qw.eq("link_erp", linkErp);
		}
		qw.eq("status", StateEnum.ENABLED.getCode());

		qw.orderByDesc("pro_time");
		PageHelper.startPage(pageIndex, limit);
		Page<ControlInvoice> iPage = controlInvoiceDao.selectPage(new Page<>((pageIndex + limit) / limit, limit), qw);
		if (!StringUtils.isNull(iPage.getRecords())) {
			PageInfo pageInfo = new PageInfo(iPage.getRecords());
			if (!StringUtils.isNull(pageInfo.getTotal()))
				iPage.setTotal(pageInfo.getTotal());
		}
		return iPage;
	}

	@Override
	public List<ControlInvoice> findList(String startTime, String endTime, Integer conAuto, Integer linkErp) {
		QueryWrapper<ControlInvoice> qw = new QueryWrapper<>();
		if (StringUtils.isNotBlank(startTime)) {
			qw.ge("pro_time", startTime);
		}
		if (StringUtils.isNotBlank(endTime)) {
			qw.lt("pro_time", endTime);
		}
		if (conAuto != null) {
			qw.eq("con_auto", conAuto);
		}
		if (linkErp != null) {
			qw.eq("con_auto", 1);
			if (linkErp == 0) {
				//	未关联
				qw.isNull("link_erp");
			} else {
				//	已关联
				qw.eq("link_erp", 1);
			}
			// qw.eq("link_erp", linkErp);
		}
		qw.eq("status", StateEnum.ENABLED.getCode());
		qw.orderByDesc("pro_time");
		List<ControlInvoice> controlInvoiceList = controlInvoiceDao.selectList(qw);
		return controlInvoiceList;
	}
}